<template>
  <el-dialog
    title="条件/动作"
    :close-on-click-modal="false"
    width="60%"
    append-to-body
    :visible.sync="visible">
    <el-form :model="dataForm" ref="dataForm">
      <!-- 条件为已定义条件-->
      <el-row>
        <el-col :span="24" class="mr10">
          <el-form-item label="因子" label-width="120px" prop="">
            <el-col :span="5">
              <el-select v-model="dataForm.divisorId" filterable disabled placeholder="请选择因子">
                <el-option
                  v-for="item in conditionDivisorList"
                  :key="item.id"
                  :label="item.divisorName"
                  :value="item.id"
                  @click.native="changeDivisor(item)">
                </el-option>
              </el-select>
            </el-col>

            <!-- 规则因子 非方法 且文本类型 则是输入框-->
            <el-col :span="5" v-if="divisor.divisorType==='0' && divisor.itemValueType==='1' && (divisor.itemTxtType==='1' || divisor.itemTxtType==='3')">
              <el-input v-model="rightValue" @blur="insertV4" placeholder="文本值"></el-input>
            </el-col>

            <!-- 规则因子 非方法 且枚举类型 则是下拉框-->
            <el-col :span="5" v-else-if="divisor.divisorType==='0' && divisor.itemValueType==='2'">
              <el-select v-model="rightValue" filterable clearable placeholder="请选择条件">
                <el-option
                  v-for="item in divisorEnumList"
                  :key="item.enumValue"
                  :label="item.enumCode"
                  :value="item.enumValue"
                @click.native="insertV2(item)">
                </el-option>
              </el-select>
            </el-col>

          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="dataForm.conditionType=='4' && divisor.divisorType==='1' && divisorMethodParams.length>0">
        <el-col :span="6" v-for="(p,index) in divisorMethodParams" :key="index">
          <el-form-item :label="p.desc" label-width="120px" prop="">
            <el-input v-model="p.className"  placeholder="值" @blur="changeDivisorParams(p)"></el-input>
          </el-form-item>
        </el-col>
      </el-row>


      <el-row>
        <el-col :span="24">
          <el-form-item label="表达式" label-width="120px" prop="">
            <el-input type="textarea" :rows="3" disabled v-model="dataForm.decisionExp"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="描述" label-width="120px" prop="">
            <el-input type="textarea" :rows="3" disabled v-model="dataForm.conditionDesc"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>


    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="getExp()">确定</el-button>
    </span>


  </el-dialog>


</template>

<script>
    import {entityItemList, getEntityMethod, entityList,getActionMethodName} from '@/api/baseConfig/entity'
    import constant from "../../../../../api/constant";
    import {listConditionDivisorData,getData} from '@/api/baseConfig/conditionDivisor'
    import {listActionDivisorData} from '@/api/baseConfig/actionDivisor'

    export default {
        data() {
            return {
                rightValue:'',
                newConditionList:[],
                divisor: {
                    configDate: null,
                    configUser: null,
                    creTime: null,
                    creUserId: null,
                    divisorIdentify: null,
                    divisorMethod: null,
                    divisorName: null,
                    divisorType: null,
                    entityId: null,
                    id: null,
                    isEffect: null,
                    itemId: null,
                    itemIdentify: null,
                    itemTxtLength: null,
                    itemTxtType: null,
                    itemValueType: null,
                    methodDesc: null,
                    methodExpression: null,
                    methodParams: null,
                    methodType: null,
                    remark: null,
                    sysbol: null
                },
                divisorEnumList:[],
                divisorMethodParams:[],
                descData: [],
                headForm:{
                    conditionDesc:''
                },
                dataForm: {
                    id:null,
                    conditionType: '4',
                    divisorId: null,
                    leftProperty: null,
                    rightProperty: null,
                    leftEntityId: null,
                    rightEntityId: null,
                    sysbol: '',
                    rightValue: '',
                    decisionExp:'',
                    conditionExpression: '',
                    conditionDesc: '',
                    conditionMethodType: '',
                    methodParams: '',
                    methodName: '',
                    remark:''
                },
                showParams: false,
                methodParams: [],
                ruleInfo: {},
                ruleCode: '',
                delInfo: [],
                inputRemark: '',
                rightValue: '',
                curEntity: {},
                methodList: [],
                entityEnum: '',
                enumList: [],
                enumFlag: false,
                entity: null,
                entityItem: null,
                ruleId: null,
                inputV: '',
                listData: [],
                entityInfos: [],
                entityItemInfos: [],
                leftEntityItemInfos: [],
                rightEntityItemInfos: [],
                visible: false,
                dialogVisible: false,
                sysbols: constant.ruleConfig.sysbols,
                conditionTyps: constant.ruleConfig.conditionTyps,
                conditionDivisorList: []
            }
        },
        props: {
            conditionList: {
                type: Array, //指定传入的类型
                default: [] //这样可以指定默认的值
            },
            entityList: {
                type: Array, //指定传入的类型
                default: [] //这样可以指定默认的值
            },
            chooseIndex: {
                type: Number,
                default: 0
            }
        },
        created() {

        },
        methods: {
            changeDivisorParams(data){
                // console.log(data)
            },
            // 选择条件因子
            changeDivisor(data) {
                // console.log(data)
                this.divisor = data
                this.divisorMethodParams = []
                this.descData = new Array(2)

                //判断因子类型是方法还是非方法
                if (data.divisorType === '0') {
                    //非方法 枚举
                    if (data.itemValueType === '2') {
                        getData({id:data.id}).then(res=>{
                          this.divisorEnumList = res.data.data.enumList
                            // console.log(this.divisorEnumList)
                        })
                    }
                    let desc = JSON.parse(data.remark)
                    let s = desc[1].k.substring(0, 1).toLocaleUpperCase() + desc[1].k.substring(1, desc[1].k.length)

                    let v = '$'+desc[0].k+'.get'+s+'() '+ desc[2].k
                    let k = desc[0].v+'的 '+desc[1].v+' '+ desc[2].v
                    this.descData[0] = {k:k,v:v}
                    this.descData[1] = {}
                    this.dataForm.rightValue = ''
                }else{
                    this.descData[0] = {k:data.methodDesc,v:'ClmUtil.' +data.methodExpression}
                    this.dataForm.conditionMethodType = data.methodType
                    this.dataForm.leftEntityId = data.entityId
                    if(data.methodParams){
                        this.divisorMethodParams = JSON.parse(data.methodParams)
                    }
                    this.dataForm.methodName = data.methodExpression
                }
                this.showValue()

            },


            // 表达式填写完毕 确定
            getExp() {
                let inputValue = this.dataForm.methodName

                 if(this.dataForm.conditionType==='4'){
                    //已定义条件
                    if(!this.dataForm.divisorId){
                        this.$message.error('请将条件补充完整' )
                        return
                    }
                    if(this.divisor.divisorType==='1'){//方法
                        let methodP = ''
                        if(this.divisorMethodParams && this.divisorMethodParams.length>0){
                            for (var i = 0; i < this.divisorMethodParams.length; i++) {
                                let item = this.divisorMethodParams[i]
                                let value = item.className
                                if (!value) {
                                    this.$message.error('请输入' + item.desc)
                                    return
                                }
                                if (item.type === 'int') {
                                    const digit = /^[0-9]*$/
                                    if (!digit.test(value)) {
                                        this.$message.error(item.desc + '只能输入数字')
                                        return
                                    }
                                } else if (item.type === 'String') {
                                    value = '"' + value + '"'
                                }
                                let p = '$' + item.name + '$'
                                inputValue = inputValue.replace(p, value)
                                methodP = methodP+ value
                                if(i<=this.divisorMethodParams.length-2){
                                    methodP = methodP + ','
                                }
                            }
                            this.dataForm.methodParams = JSON.stringify(this.divisorMethodParams)
                            if (this.divisor.methodType === '0' || this.divisor.methodType === '1' || this.divisor.methodType === '2') {
                                this.dataForm.conditionMethodType = '0'
                            } else if (this.divisor.methodType === '3') {
                                this.dataForm.conditionMethodType = '1'
                            }
                        }
                        this.dataForm.decisionExp = 'ClmUtil.' +inputValue
                        this.dataForm.conditionDesc = methodP
                    }else{
                        this.dataForm.leftEntityId = this.divisor.entityId
                        this.dataForm.leftProperty = this.divisor.itemId
                        this.dataForm.sysbol = this.divisor.sysbol
                        if(!this.rightValue){
                            this.$message.error('请将条件值补充完整' )
                            return
                        }
                        if(this.divisor.itemValueType==='1' && this.rightValue.length>this.divisor.itemTxtLength){
                            this.$message.error('条件值长度不能大于'+this.divisor.itemTxtLength )
                            return
                        }
                        this.dataForm.rightValue = this.rightValue
                        this.dataForm.conditionDesc = this.rightValue

                    }
                }

                this.visible = false
                this.dataForm.remark = JSON.stringify(this.descData)
                this.$emit('refreshDataList',this.dataForm)
            },


            // 显示描述信息
            showValue() {
                this.dataForm.conditionExpression = ''
                this.dataForm.conditionDesc = ''
                this.descData.forEach(item => {
                    if(item.v && item.k){
                        this.dataForm.conditionExpression = this.dataForm.conditionExpression + item.v
                        this.dataForm.conditionDesc = this.dataForm.conditionDesc + item.k + ' '
                    }
                })
            },


            // 条件因子枚举值选择
            insertV2(item) {
                this.descData[0] = {k:item.enumCode,v:this.rightValue}
                // this.showValue()
                this.dataForm.decisionExp = this.dataForm.conditionExpression + this.rightValue
            },

            //条件因子值输入
            insertV4() {
                this.descData[0] = {k:this.rightValue,v:this.rightValue}
                this.dataForm.decisionExp = this.dataForm.conditionExpression + this.rightValue
                let head = this.headForm.conditionDesc
                this.dataForm.conditionDesc = head+this.rightValue
            },

            init(colData) {
                this.visible = true

                this.newConditionList = []
                this.divisor = {
                    configDate: null,
                    configUser: null,
                    creTime: null,
                    creUserId: null,
                    divisorIdentify: null,
                    divisorMethod: null,
                    divisorName: null,
                    divisorType: null,
                    entityId: null,
                    id: null,
                    isEffect: null,
                    itemId: null,
                    itemIdentify: null,
                    itemTxtLength: null,
                    itemTxtType: null,
                    itemValueType: null,
                    methodDesc: null,
                    methodExpression: null,
                    methodParams: null,
                    methodType: null,
                    remark: null,
                    sysbol: null
                }
                this.dataForm = {
                    id:null,
                    conditionType: '4',
                    divisorId: null,
                    leftProperty: null,
                    rightProperty: null,
                    leftEntityId: null,
                    rightEntityId: null,
                    sysbol: '',
                    rightValue: '',
                    decisionExp:'',
                    conditionExpression: '',
                    conditionDesc: '',
                    conditionMethodType: '',
                    methodParams: '',
                    methodName: '',
                    remark:''
                }
                this.entityInfos = []
                this.rightValue = ''
                this.delInfo = []
                this.methodList = []
                this.methodParams = []
                this.divisorMethodParams = []
                this.showParams = false
                this.conditionType = ''
                this.descData = []

                // 获取表头信息
                this.newConditionList = JSON.parse(JSON.stringify(this.conditionList))
                if(this.chooseIndex!=null && this.conditionList.length>0){
                    this.dataForm = this.newConditionList[this.chooseIndex]
                    this.headForm = JSON.parse(JSON.stringify(this.newConditionList[this.chooseIndex]))
                }

                // 获取值信息
                if(colData && ! colData.conditionDesc){
                    // console.log(colData)
                    let vData = JSON.parse(colData)
                    if(vData.rightEntityId){
                        this.dataForm.rightEntityId = parseInt(vData.rightEntityId)
                    }
                    if(vData.rightProperty){
                        this.dataForm.rightProperty = parseInt(vData.rightProperty)
                    }

                    if(vData.id){
                        this.dataForm.id = vData.id
                    }
                    if(vData.sysbol){
                        this.dataForm.sysbol = vData.sysbol
                    }
                    if(vData.rightValue){
                        this.dataForm.rightValue = vData.rightValue
                        this.rightValue = vData.rightValue
                    }


                    if(vData.conditionExpression){
                        this.dataForm.conditionExpression = vData.conditionExpression
                    }
                    if(vData.conditionDesc){
                        this.dataForm.conditionDesc = vData.conditionDesc
                    }
                    if(vData.methodParams){
                        this.dataForm.methodParams = vData.methodParams
                    }
                    if(vData.divisorId){
                        this.dataForm.divisorId = vData.divisorId
                    }
                    this.dataForm.remark = vData.remark
                    if(vData.remark){
                        this.descData = JSON.parse(vData.remark)
                    }
                    if(!vData.decisionExp){
                        this.dataForm.decisionExp = this.headForm.conditionExpression
                    }else{
                        this.dataForm.decisionExp = vData.decisionExp
                    }
                }

                this.initData()
                // console.log(this.dataForm.rightValue)
            },

            // 获取规则因子,实体类
            initData() {
                let _that = this
                let params = {
                    is_effect: '1'
                }
                _that.descData = new Array(2)
                if(_that.dataForm.headType==='1'){
                    _that.sysbols = constant.ruleConfig.sysbols
                    listConditionDivisorData(params).then(res => {
                        _that.conditionDivisorList = res.data.data
                        if(_that.dataForm.divisorId){
                            _that.conditionDivisorList.forEach(item=>{
                                if(item.id === _that.dataForm.divisorId){
                                    _that.divisor = item
                                    if(_that.divisor.divisorType ==='0'){
                                        this.dataForm.conditionDesc = this.headForm.conditionDesc + this.rightValue
                                    }else{
                                        this.dataForm.conditionDesc = this.headForm.conditionDesc
                                    }
                                    // console.log(_that.divisor)
                                }
                            })
                            if(_that.dataForm.conditionType==='4'){
                                if(_that.divisor.divisorType==='1' && _that.dataForm.methodParams) {//方法
                                    _that.divisorMethodParams = JSON.parse(_that.dataForm.methodParams)
                                }
                            }
                        }
                    })
                }else{
                    _that.sysbols = constant.ruleConfig.actionSysbols
                    listActionDivisorData(params).then(res => {
                        _that.conditionDivisorList = res.data.data
                        if(_that.dataForm.divisorId){
                            _that.conditionDivisorList.forEach(item=>{
                                if(item.id === _that.dataForm.divisorId){
                                    _that.divisor = item
                                }
                            })
                            if(_that.dataForm.conditionType==='4'){
                                if(_that.divisor.divisorType==='1' && _that.dataForm.methodParams) {//方法
                                    _that.divisorMethodParams = JSON.parse(_that.dataForm.methodParams)
                                }
                            }
                        }
                    })
                }

                if(_that.entityList.length>0){
                    _that.entityInfos = this.entityList
                }else{
                    entityList(params).then(res => {
                        _that.entityInfos = res.data.data
                    })
                }
            },

        }
    }
</script>

<style lang="scss">
  .title {
    padding: 10px;
    background-color: rgba(248, 248, 248, 1);
    margin-bottom: 10px;
  }

  .el-table__row .el-form-item__content {
    margin-left: 0 !important;
  }

  .el-table__row .el-form-item {
    margin-bottom: 0 !important;
  }

  .btn-span {
    display: block;
    text-align: center;
    border: 1px solid #dcdfe6;
    border-radius: 5px;
    cursor: pointer;
  }

  .mr10 {
    margin-right: 10px;
  }
</style>
